package com.lambkit.module.center.controller;

import java.util.Date;
import java.util.List;

import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.lambkit.db.mgr.MgrTable;
import com.lambkit.module.meta.model.MetaFileCatalog;
import com.lambkit.module.meta.service.MetaFileCatalogService;
import com.lambkit.web.controller.LambkitController;

public class FileCatalogController extends LambkitController {

	/**
	 * 获取某一folder的信息
	 */
	public void index() {
		MetaFileCatalogService service = MetaFileCatalog.service();
		if(service==null) {
			renderJson(Ret.fail().set("msg", "service is null"));
			return;
		}
		MgrTable tbc = getTableView(service.getTableName(), true);
		if (tbc == null) {
			renderJson(Ret.fail().set("msg", "table is not find."));
			return;
		}
		String model_id = getPara(0, getPara("id"));
		if (StrKit.notBlank(model_id)) {
			MetaFileCatalog model = service.findById(Long.valueOf(model_id));
			MetaFileCatalog pModel = null;
			String pid = getPara(1, getPara("pid"));
			if(StrKit.notBlank(pid)) {
				if(model.getPid()!=null || model.getPid()>0) {
					pModel = service.findById(model.getPid());
				}
			}
			renderJson(Ret.ok().set("data", model).set("pm", pModel));
		} else {
			renderJson(Ret.fail().set("msg", "id is null"));
		}
	}

	/**
	 * 获取folder列表
	 */
	public void list() {
		/*
		MetaFileCatalogService service = MetaFileCatalog.service();
		if(service==null) {
			renderJson(Ret.fail().set("msg", "service is null"));
			return;
		}
		MgrTable tbc = getTableView(service.getTableName(), true);
		if (tbc == null) {
			renderJson(Ret.fail().set("msg", "table is not find."));
			return;
		}
		List<Object> params = Lists.newArrayList();
		Long pid = getParaToLong(0,0L);
		SqlBuilder sb = new SqlBuilder();
		sb.appendSelect(tbc).appendFrom(tbc).appendIfNotEmpty("pid", pid, params, false);
		if(hasUser()) {
			sb.appendIfNotEmpty("user_id", Long.valueOf(getUser().getId().toString()), params, false);
		} else {
			sb.append(" and user_id is null ");
		}
		String sql =sb.build();
		List<Record> list = Db.find(sql, params);
		renderJson(Ret.ok().set("data", list));
		*/
		MetaFileCatalogService service = MetaFileCatalog.service();
		Long pid = getParaToLong(0,0L);
		Long userId = null;
		if(hasUser()) {
			userId = Long.valueOf(getUser().getId().toString());
			if(userId==1) userId = null;
		}
		List<Record> list = service.getAll(pid, userId);
		renderJson(Ret.ok().set("data", list));
	}
	
	public void link() {
		MetaFileCatalogService service = MetaFileCatalog.service();
		List<MetaFileCatalog> list = service.getFolderLink(null, getParaToLong(0,0L));
		renderJson(Ret.ok().set("data", list));
	}
	
	public void catalog() {
		Long pid = getParaToLong("pid");
		if(pid==null) {
			renderJson(Ret.fail().set("msg", "pid is null"));
			return;
		}
		Long userId = null;
		if(hasUser()) {
			userId = Long.valueOf(getUser().getId().toString());
		}
		List<MetaFileCatalog> list = MetaFileCatalog.service().getFolders(userId, pid);
		renderJson(Ret.ok().set("data", list));
	}
	
	/**
	 * 新建文件夹
	 */
	public void save() {
		MetaFileCatalog catalog = getModel(MetaFileCatalog.class, "model");
		Long userId = null;
		if(hasUser()) {
			userId = Long.valueOf(getUser().getId().toString());
			catalog.setUserId(userId);
		}
		catalog.setCreated(new Date());
		boolean flag = catalog.save();
		if(flag) {
			renderJson(Ret.ok());
		} else {
			renderJson(Ret.fail());
		}
	}
	
	public void rename() {
		Long id = getParaToLong("id");
		if(id==null) {
			renderJson(Ret.fail().set("msg", "id is null"));
			return;
		}
		String title = getPara("title");
		if(StrKit.isBlank(title)) {
			renderJson(Ret.fail().set("msg", "title is null"));
			return;
		}
		MetaFileCatalog catalog = MetaFileCatalog.service().findById(id);
		catalog.setTitle(title);
		boolean flag = catalog.update();
		if(flag) {
			renderJson(Ret.ok());
		} else {
			renderJson(Ret.fail());
		}
	}
	
	/**
	 * 删除目录和file
	 */
	public void delete() {
		String ids = getPara(0);
		int n = MetaFileCatalog.service().delete(ids);
		if(n > 0) {
			renderJson(Ret.ok().set("num", n));
		} else {
			renderJson(Ret.fail());
		}
	}
	
	/**
	 * 删除目录，不删除file
	 */
	public void remove() {
		String ids = getPara(0);
		int n = MetaFileCatalog.service().remove(ids);
		if(n > 0) {
			renderJson(Ret.ok().set("num", n));
		} else {
			renderJson(Ret.fail());
		}
	}
	
	public void move() {
		Long pid = getParaToLong("pid");
		String ids = getPara("ids");
		if(pid==null) {
			renderJson(Ret.fail().set("msg", "pid is null"));
			return;
		}
		if(StrKit.isBlank(ids)) {
			renderJson(Ret.fail().set("msg", "ids is null"));
			return;
		}
		int n = MetaFileCatalog.service().move(pid, ids);
		if(n > 0) {
			renderJson(Ret.ok().set("num", n));
		} else {
			renderJson(Ret.fail());
		}
	}

}
